*********************************************************
* Analysis Part 3: Table 9 Column 1
* 
* Topic: The Value of Ratings: Evidence from their Introduction in Securities Markets
* Authors: Asaf Bernstein, Carola Frydman, Eric Hilt
* 
* Code last updated: 5/23/25
* 
*************************************************************

*************************************************************
* I. Initial Code Settings/Directories
*************************************************************

clear all
* Set to folder of where do file is saved. Change if necessary.
cd "`=substr(c(current_do), 1, indexnot(c(current_do), "/", -1))'"

log using "Part3_AnyTrade_Log.txt", text replace

*************************************************************
* II. Load in data & define variable
*************************************************************
	
use "IntradayData.dta", clear

	bys id_issue_bidask date_bidask: egen mn_sales_tras = mean(sales_tras)	
	duplicates drop id_issue_bidask date_bidask, force
	xtset id_issue_bidask date_bidask
	tsfill, full		

    *All bonds that traded during 2-yr. window around ratings are listed on below date as a placeholder (for tsfill) - no actual trades
    drop if date_bidask==-18432

* Merge in instrument
	
	merge m:1 id_issue_bidask using "spreads_instrument_bidask_data_cross-section.dta"
	keep if _merge==3
	drop _merge	

* Create column 1 outcome

	gen has_any_trans = mn_sales_tras!=.
	bys date_bidask: egen mx_has_any_trans = max(has_any_trans)
	keep if mx_has_any_trans==1
    label variable has_any_trans "any transaction"
	
* Create main interactions for regression

	sort date_bidask
	egen date_std = group(date_bidask)
	
	gen Post = date_std>=14
	
	gen post_rated = Post*rated_issue_manual
	gen trend_rated = date_std*rated_issue_manual
	
	gen post_othry = Post*othry
	gen trend_othry = date_std*othry
	
	gen weeksSinceTreat = date_std - 13
	
	
*************************************************************
* III. Analysis/output
*************************************************************

*************************************************************
* Table 9 (column 1 only)
*************************************************************

* Column 1

    ivreghdfe has_any_trans (post_rated trend_rated = post_othry trend_othry), absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)			
    est save t9c1, replace
    
    *F stat
    matrix first_stage = e(widstat)
	local first_f = first_stage[1,1]
	display "Kleibergen-Paap F, First Stage: " `first_f'
    *Mean of dv; number of bonds (rated and unrated)
    preserve
	   keep if e(sample)==1
	   quietly sum has_any_trans
	   local mean_y = r(mean)
	   display "Mean dep var: "  `mean_y'	
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore



log close






